<template>
  <div class="mod-config">
    <el-form :inline="true" :model="queryForm" @keyup.enter.native="getDataList()">
      <!--<el-form-item>
        <el-input v-model="queryForm.floor" placeholder="楼/楼层" clearable/>
      </el-form-item>
      <el-form-item>
        <el-input v-model="queryForm.name" placeholder="房类" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>-->
      <el-form-item style="float: right">
        <el-button v-if="isAuth('tra:roomtype:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <!--        <el-button v-if="isAuth('tra:roomtype:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      :header-cell-style="{background:'#F5F7FA',color:'#606266'}"
      row-key="id"
      v-loading="dataListLoading"
      style="width: 100%;">
      <!--<el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>-->
      <el-table-column
        prop="floor"
        header-align="center"
        align="center"
        label="楼/楼层">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="房类">
      </el-table-column>
      <el-table-column
        prop="price"
        header-align="center"
        align="center"
        label="价格">
      </el-table-column>
      <el-table-column
        prop="disPrice"
        header-align="center"
        align="center"
        label="节日价格">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        label="修改时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="primary" plain size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="danger" plain size="small" @click="deleteHandle(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" :superior="this.superior"
                   @refreshDataList="getDataList"/>
  </div>
</template>
<script>
  import AddOrUpdate from './roomtype-add-or-update'
  import {treeDataTranslate} from '@/utils'

  export default {
    data () {
      return {
        queryForm: {
          floor: '',
          name: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        superior: [],
        ids: []
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/tra/roomtype/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'floor': this.queryForm.floor,
            'name': this.queryForm.name
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = treeDataTranslate(data.list)
            this.setSuperiors(data.list)
          } else {
            this.dataList = []
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (row) {
        this.idMerge(row)
        let ids = this.ids
        let message = ''
        if (ids.length > 1) {
          message = `确定对[${row.floor}]及其下所有房型删除吗?`
        } else {
          message = `确定将[${row.name}]删除吗?`
        }
        this.$confirm(message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/tra/roomtype/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      idMerge (row) {
        this.ids = []
        this.ids.push(row.id)
        if (row.children) {
          for (let children of row.children) {
            this.ids.push(children.id)
          }
        }
      },
      /**
       * 新增修改页面树级数据获取
       * @param roomTypes
       */
      setSuperiors (roomTypes) {
        this.superior = []
        for (let roomType of roomTypes) {
          if (roomType.type === 0) {
            this.superior.push({label: roomType.floor, value: roomType.id})
          }
        }
      }
    }
  }
</script>
